import com.proalias.animation.TweenManager;

import flash.geom.ColorTransform;
import flash.geom.Transform;
/**
 * @author Alias Cummins
 */
class com.proalias.animation.tween.ColorTween {
	
	private var $target:Object;
	private var $time:Number;
	private var $duration:Number;
	private var $startValues:Object;
	private var $end:Number;
	private var $preCalc:Boolean;
	private var $preCalcObj:Object;
	private var $easingFunction:Function;
	private var $properties:Object;
	
	public var execute:Function;
	private var $startCtx : ColorTransform;


	private var $startTransform : ColorTransform;
	private var $targetTransform : ColorTransform;
	private var $destTransform : ColorTransform;

	private var $transform : Transform;

	
	public function ColorTween(target:MovieClip,colorTransform:ColorTransform,easing:String,duration:Number) {
		$target = target;
		$time = 0;
		$duration = int(duration);

		$destTransform = colorTransform;
		
		$transform = new Transform(target);
		$targetTransform = $transform.colorTransform;
		$startTransform = new ColorTransform();
		for (var i:String in $targetTransform){
				//trace("ColorTween.ColorTween::$targetTransform["+i+"]="+$targetTransform[i]);
				$startTransform[i] = $targetTransform[i];
				//trace("ColorTween.ColorTween::$destTransform["+i+"]="+$destTransform[i]);
		} 
		
		var e:Array = easing.split(".");
		
		$easingFunction = TweenManager.instance.easingLib[e[0]][e[1]];
		execute = executeRealTime;
	}
	
	
	private function executeRealTime():Boolean{
		//trace("Tween.executeRealTime()");
		var func:Function = $easingFunction;

		if ($time < $duration){
			
			for (var i in $destTransform){
				if (typeof($targetTransform[i])=="number" && i != "rgb"){
					var b:Number = $startTransform[i];
					var c:Number = $destTransform[i] - b;
					var d:Number = $duration;
					var p:Number = func($time, b, c, d);
					$targetTransform[i] = p;
					$transform.colorTransform = $targetTransform;
				}
			}
			$time++;
			return true;
		}else{
			$targetTransform = $destTransform;
			execute = null;	
			return false;
		}
		
	}
	
	private function executePreCalc():Boolean{
		//trace("Tween.executePreCalc()");
		if ($time < $duration){
			for (var i in $properties){
				$target[i] = $preCalcObj[i][$time];
			}
			$time++;
			return true;
		}else{
			for (var i in $properties){
				$target[i] = $properties[i];
			}
			return false;
		}
		return true;
	}
	
}